#include <stdio.h>
#include <stdlib.h>

int matrix[100][100];
int rhs[100];

int n;
int p;

void swap_rows(int i1, int i2)
{
	int t;
	for(int ii = 0; ii < n; ii++)
	{
		t = matrix[i1][ii];
		matrix[i1][ii] = matrix[i2][ii];
		matrix[i2][ii] = t;
	}

	t = rhs[i1];
	rhs[i1] = rhs[i2];
	rhs[i2] = t;
}

void subtract_row_multiple(int i1, int i2, int r)
{
	for(int ii = 0; ii < n; ii++)
	{
		matrix[i2][ii] = (matrix[i2][ii] - matrix[i1][ii] * r) % p;
	}

	rhs[i2] = (rhs[i2] - rhs[i1] * r) % p;
}

void mul_row(int i1, int r)
{
	for(int ii = 0; ii < n; ii++)
	{
		matrix[i1][ii] = (matrix[i1][ii] * r) % p;
	}
	rhs[i1] = (rhs[i1] * r) % p;
}

int inverse(int r)
{
	int a, b, x[2], y[2], z[2];
	a = r, b = p;
	x[0] = 1, x[1] = 0;
	y[0] = 0, y[1] = 1;
	z[0] = r, z[1] = p;

	while(z[1] != 0)
	{
		int d = z[0] / z[1];
		int xt = x[0] - d * x[1];
		int yt = y[0] - d * y[1];
		int zt = z[0] - d * z[1];

		x[0] = x[1], y[0] = y[1], z[0] = z[1];
		x[1] = xt, y[1] = yt, z[1] = zt;
	}

	return x[0];
}

void print_matrix()
{

	for(int ii = 0; ii < n; ii++)
	{
		for(int jj = 0; jj < n; jj++)
			printf("%d ", matrix[ii][jj]);
		printf("\n");
	}
}

void solve()
{
	for(int ii = 0; ii < n; ii++)
	{
		// Find a row with a non-zero here
		int jj;
		for(jj = ii; jj < n; jj++)
			if(matrix[jj][ii] != 0) break;

		if(jj != ii)
			swap_rows(ii, jj);

		int r = matrix[ii][ii] % p;
		if(r < 0) r += p;
		
		int rt = inverse(r);
		//printf("rt is %d\n", rt);
		mul_row(ii, rt);

		// Now fix all the other (non-zero) rows
		for(jj = ii+1; jj < n; jj++)
			subtract_row_multiple(ii, jj, matrix[jj][ii]);

		//printf("\nAfter %d\n", ii);
		//print_matrix();
	}

	for(int ii = n-1; ii >= 0; ii--)
	{
		for(int jj = 0; jj < ii; jj++)
		{
			subtract_row_multiple(ii, jj, matrix[jj][ii]);
		}
	}

	//print_matrix();
}

int one_case()
{
	scanf("%d", &p);
	char c = getchar();
	for(n = 0; c != '\n'; n++)
	{
		c = getchar();
		rhs[n] = c == '*' ? 0 : c - 'a' + 1;
	}
	n--;
	
	/*
	for(int ii = 0; ii < n; ii++)
	{
		int r = rhs[ii] % p;
		if(r < 0) r += p;
		printf(ii == n-1 ? "%d\n" : "%d ", r);
	}
	*/

	for(int ii = 0; ii < n; ii++)
	for(int jj = 0; jj < n; jj++)
	{
		matrix[ii][jj] = jj == 0 ? 1 : (matrix[ii][jj-1] * (ii+1)) % p;
	}

	solve();

	for(int ii = 0; ii < n; ii++)
	{
		int r = rhs[ii] % p;
		if(r < 0) r += p;
		printf(ii == n-1 ? "%d\n" : "%d ", r);
	}

	return 0;
}

int main()
{
	int no_cases;
	scanf("%d", &no_cases);
	for(int ii = 0; ii < no_cases; ii++)
		one_case();

	return 0;
}
